home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 176-200 / disk_195 / microemacs / src.zoo / file.c < prev    next >
C/C++ Source or Header  |  1989-03-23  |  18KB  |  745 lines

  1. /*    FILE.C:   for MicroEMACS
  2.  
  3.     The routines in this file handle the reading, writing
  4.     and lookup of disk files.  All of details about the
  5.     reading and writing of the disk are in "fileio.c".
  6.  
  7. */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "etype.h"
  12. #include    "edef.h"
  13. #include    "elang.h"
  14.  
  15. /*
  16.  * Read a file into the current
  17.  * buffer. This is really easy; all you do is
  18.  * find the name of the file, and call the standard
  19.  * "read a file into the current buffer" code.
  20.  * Bound to "C-X C-R".
  21.  */
  22. PASCAL NEAR fileread(f, n)
  23.  
  24. int f, n;    /* defualt and numeric arguments (unused) */
  25.  
  26. {
  27.     register int s;    /* status return */
  28.     char *fname;    /* file name to read */
  29.  
  30.     if (restflag)        /* don't allow this command if restricted */
  31.         return(resterr());
  32.  
  33.     if ((fname = gtfilename(TEXT131)) == NULL)
  34. /*                              "Read file" */
  35.         return(FALSE);
  36.     return(readin(fname, TRUE));
  37. }
  38.  
  39. /*
  40.  * Insert a file into the current
  41.  * buffer. This is really easy; all you do it
  42.  * find the name of the file, and call the standard
  43.  * "insert a file into the current buffer" code.
  44.  * Bound to "C-X C-I".
  45.  */
  46. PASCAL NEAR insfile(f, n)
  47. {
  48.     register int    s;
  49.     char *fname;    /* file name */
  50.  
  51.     if (restflag)        /* don't allow this command if restricted */
  52.         return(resterr());
  53.     if (curbp->b_mode&MDVIEW)      /* don't allow this command if  */
  54.         return(rdonly());    /* we are in read only mode    */
  55.  
  56.     if ((fname = gtfilename(TEXT132)) == NULL) 
  57. /*                              "Insert file" */
  58.         return(FALSE);
  59.     return(ifile(fname));
  60. }
  61.  
  62. /*
  63.  * Select a file for editing.
  64.  * Look around to see if you can find the
  65.  * fine in another buffer; if you can find it
  66.  * just switch to the buffer. If you cannot find
  67.  * the file, create a new buffer, read in the
  68.  * text, and switch to the new buffer.
  69.  * Bound to C-X C-F.
  70.  */
  71. PASCAL NEAR filefind(f, n)
  72. {
  73.     char *fname;    /* file user wishes to find */    /* file name */
  74.     register int s;        /* status return */
  75.  
  76.     if (restflag)        /* don't allow this command if restricted */
  77.         return(resterr());
  78.  
  79.     if ((fname = gtfilename(TEXT133)) == NULL) 
  80. /*                              "Find file" */
  81.         return(FALSE);
  82.     return(getfile(fname, TRUE));
  83. }
  84.  
  85. PASCAL NEAR viewfile(f, n)    /* visit a file in VIEW mode */
  86. {
  87.     char *fname;    /* file user wishes to find */    /* file name */
  88.     register int s;    /* status return */
  89.  
  90.     if (restflag)        /* don't allow this command if restricted */
  91.         return(resterr());
  92.  
  93.     if ((fname = gtfilename(TEXT134)) == NULL) 
  94. /*                              "View file" */
  95.         return(FALSE);
  96.     s = getfile(fname, FALSE);
  97.     if (s) {    /* if we succeed, put it in view mode */
  98.         curwp->w_bufp->b_mode |= MDVIEW;
  99.         upmode();
  100.     }
  101.     return(s);
  102. }
  103.  
  104. #if    CRYPT
  105. PASCAL NEAR resetkey()    /* reset the encryption key if needed */
  106.  
  107. {
  108.     register int s; /* return status */
  109.  
  110.     /* turn off the encryption flag */
  111.     cryptflag = FALSE;
  112.  
  113.     /* if we are in crypt mode */
  114.     if (curbp->b_mode & MDCRYPT) {
  115.         if (curbp->b_key[0] == 0) {
  116.             s = setekey(FALSE, 0);
  117.             if (s != TRUE)
  118.                 return(s);
  119.         }
  120.  
  121.         /* let others know... */
  122.         cryptflag = TRUE;
  123.  
  124.         /* and set up the key to be used! */
  125.         /* de-encrypt it */
  126.         crypt((char *)NULL, 0);
  127.         crypt(curbp->b_key, strlen(curbp->b_key));
  128.  
  129.         /* re-encrypt it...seeding it to start */
  130.         crypt((char *)NULL, 0);
  131.         crypt(curbp->b_key, strlen(curbp->b_key));
  132.     }
  133.  
  134.     return(TRUE);
  135. }
  136. #endif
  137.  
  138. PASCAL NEAR getfile(fname, lockfl)
  139.  
  140. char fname[];        /* file name to find */
  141. int lockfl;        /* check the file for locks? */
  142.  
  143. {
  144.     register BUFFER *bp;
  145.     register LINE    *lp;
  146.     register int    i;
  147.     register int    s;
  148.     register int cmark;    /* current mark */
  149.     char bname[NBUFN];    /* buffer name to put file */
  150.  
  151. #if    MSDOS | OS2 | AOSVS
  152.     mklower(fname);            /* msdos isn't case sensitive */
  153. #endif
  154.     for (bp=bheadp; bp!=NULL; bp=bp->b_bufp) {
  155.         if ((bp->b_flag&BFINVS)==0 && strcmp(bp->b_fname, fname)==0) {
  156.             swbuffer(bp);
  157.             lp = curwp->w_dotp;
  158.             i = curwp->w_ntrows/2;
  159.             while (i-- && lback(lp)!=curbp->b_linep)
  160.                 lp = lback(lp);
  161.             curwp->w_linep = lp;
  162.             curwp->w_flag |= WFMODE|WFHARD;
  163.             mlwrite(TEXT135);
  164. /*                              "[Old buffer]" */
  165.             return(TRUE);
  166.         }
  167.     }
  168.     makename(bname, fname);         /* New buffer name.    */
  169.     while ((bp=bfind(bname, FALSE, 0)) != NULL) {
  170.         /* old buffer name conflict code */
  171.         s = mlreply(TEXT136, bname, NBUFN);
  172. /*                          "Buffer name: " */
  173.         if (s == ABORT)         /* ^G to just quit    */
  174.             return(s);
  175.         if (s == FALSE) {        /* CR to clobber it    */
  176.             makename(bname, fname);
  177.             break;
  178.         }
  179.     }
  180.     if (bp==NULL && (bp=bfind(bname, TRUE, 0))==NULL) {
  181.         mlwrite(TEXT137);
  182. /*                      "Cannot create buffer" */
  183.         return(FALSE);
  184.     }
  185.     if (--curbp->b_nwnd == 0) {        /* Undisplay.        */
  186.         curbp->b_dotp = curwp->w_dotp;
  187.         curbp->b_doto = curwp->w_doto;
  188.         for (cmark = 0; cmark < NMARKS; cmark++) {
  189.             curbp->b_markp[cmark] = curwp->w_markp[cmark];
  190.             curbp->b_marko[cmark] = curwp->w_marko[cmark];
  191.         }
  192.         curbp->b_fcol = curwp->w_fcol;
  193.     }
  194.     curbp = bp;                /* Switch to it.    */
  195.     curwp->w_bufp = bp;
  196.     curbp->b_nwnd++;
  197.     return(readin(fname, lockfl));        /* Read it in.        */
  198. }
  199.  
  200. /*
  201.     Read file "fname" into the current buffer, blowing away any text
  202.     found there.  Called by both the read and find commands.  Return
  203.     the final status of the read.  Also called by the mainline, to
  204.     read in a file specified on the command line as an argument. 
  205.     The command in $readhook is called after the buffer is set up
  206.     and before it is read. 
  207. */
  208.  
  209. PASCAL NEAR readin(fname, lockfl)
  210.  
  211. char    fname[];    /* name of file to read */
  212. int    lockfl;        /* check for file locks? */
  213.  
  214. {
  215.     register LINE *lp1;
  216.     register LINE *lp2;
  217.     register int i;
  218.     register WINDOW *wp;
  219.     register BUFFER *bp;
  220.     register int s;
  221.     register int nbytes;
  222.     register int nline;
  223.     register int cmark;    /* current mark */
  224.     char mesg[NSTRING];
  225.  
  226. #if    FILOCK
  227.     if (lockfl && lockchk(fname) == ABORT)
  228.         return(ABORT);
  229. #endif
  230.     bp = curbp;                /* Cheap.        */
  231.     if ((s=bclear(bp)) != TRUE)        /* Might be old.    */
  232.         return(s);
  233.     bp->b_flag &= ~(BFINVS|BFCHG);
  234.     strcpy(bp->b_fname, fname);
  235.  
  236.     /* let a user macro get hold of things...if he wants */
  237.     execkey(&readhook, FALSE, 1);
  238.  
  239. #if    CRYPT
  240.     /* set up for decryption */
  241.     s = resetkey();
  242.     if (s != TRUE)
  243.         return(s);
  244. #endif
  245.  
  246.     /* turn off ALL keyboard translation in case we get a dos error */
  247.     TTkclose();
  248.  
  249.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  250.         goto out;
  251.  
  252.     if (s == FIOFNF) {            /* File not found.    */
  253.         mlwrite(TEXT138);
  254. /*                      "[New file]" */
  255.         goto out;
  256.     }
  257.  
  258.     /* read the file in */
  259.     mlwrite(TEXT139);
  260. /*              "[Reading file]" */
  261.     nline = 0;
  262.     while ((s=ffgetline()) == FIOSUC) {
  263.         nbytes = strlen(fline);
  264.         if ((lp1=lalloc(nbytes)) == NULL) {
  265.             s = FIOMEM;        /* Keep message on the    */
  266.             break;            /* display.        */
  267.         }
  268.         lp2 = lback(curbp->b_linep);
  269.         lp2->l_fp = lp1;
  270.         lp1->l_fp = curbp->b_linep;
  271.         lp1->l_bp = lp2;
  272.         curbp->b_linep->l_bp = lp1;
  273.         for (i=0; i<nbytes; ++i)
  274.             lputc(lp1, i, fline[i]);
  275.         ++nline;
  276.     }
  277.     ffclose();                /* Ignore errors.    */
  278.     strcpy(mesg, "[");
  279.     if (s==FIOERR) {
  280.         strcat(mesg, TEXT141);
  281. /*                           "I/O ERROR, " */
  282.         curbp->b_flag |= BFTRUNC;
  283.     }
  284.     if (s == FIOMEM) {
  285.         strcat(mesg, TEXT142);
  286. /*                           "OUT OF MEMORY, " */
  287.         curbp->b_flag |= BFTRUNC;
  288.     }
  289.     strcat(mesg, TEXT140);
  290. /*                   "Read " */
  291.     strcat(mesg, int_asc(nline));
  292.     strcat(mesg, TEXT143);
  293. /*                   " line" */
  294.     if (nline > 1)
  295.         strcat(mesg, "s");
  296.     strcat(mesg, "]");
  297.     mlwrite(mesg);
  298.  
  299. out:
  300.     TTkopen();    /* open the keyboard again */
  301.     for (wp=wheadp; wp!=NULL; wp=wp->w_wndp) {
  302.         if (wp->w_bufp == curbp) {
  303.             wp->w_linep = lforw(curbp->b_linep);
  304.             wp->w_dotp  = lforw(curbp->b_linep);
  305.             wp->w_doto  = 0;
  306.             for (cmark = 0; cmark < NMARKS; cmark++) {
  307.                 wp->w_markp[cmark] = NULL;
  308.                 wp->w_marko[cmark] = 0;
  309.             }
  310.             wp->w_flag |= WFMODE|WFHARD;
  311.         }
  312.     }
  313.     if (s == FIOERR || s == FIOFNF)     /* False if error.    */
  314.         return(FALSE);
  315.     return(TRUE);
  316. }
  317.  
  318. /*
  319.  * Take a file name, and from it
  320.  * fabricate a buffer name. This routine knows
  321.  * about the syntax of file names on the target system.
  322.  * I suppose that this information could be put in
  323.  * a better place than a line of code.
  324.  * Returns a pointer into fname indicating the end of the file path; i.e.,
  325.  * 1 character BEYOND the path name.
  326.  */
  327. char *PASCAL NEAR makename(bname, fname)
  328. char    bname[];
  329. char    fname[];
  330. {
  331.     register char *cp1;
  332.     register char *cp2;
  333.     register char *pathp;
  334.  
  335. #if     AOSVS | MV_UX
  336.         resolve_full_pathname(fname, fname);
  337.         mklower(fname);   /* aos/vs not case sensitive */
  338. #endif
  339.     cp1 = &fname[0];
  340.     while (*cp1 != 0)
  341.         ++cp1;
  342.  
  343. #if    AMIGA
  344.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='/')
  345.         --cp1;
  346. #endif
  347. #if     AOSVS | MV_UX
  348.         while (cp1!=&fname[0] && cp1[-1]!=':')
  349.                 --cp1;
  350. #endif
  351. #if    VMS
  352.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!=']')
  353.         --cp1;
  354. #endif
  355. #if    MSDOS | MSDOS
  356.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  357.         --cp1;
  358. #endif
  359. #if    ST520
  360.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\')
  361.         --cp1;
  362. #endif
  363. #if    FINDER
  364.     while (cp1!=&fname[0] && cp1[-1]!=':' && cp1[-1]!='\\'&&cp1[-1]!='/')
  365.         --cp1;
  366. #endif
  367. #if    V7 | USG | HPUX | BSD | SUN | XENIX
  368.     while (cp1!=&fname[0] && cp1[-1]!='/')
  369.         --cp1;
  370. #endif
  371. #if WMCS
  372.     while (cp1!=&fname[0] && cp1[-1]!='_' && cp1[-1]!='/')
  373.         --cp1;
  374. #endif
  375.     /* cp1 is pointing to the first real filename char */
  376.     pathp = cp1;
  377.  
  378.     cp2 = &bname[0];
  379.     while (cp2!=&bname[NBUFN-1] && *cp1!=0 && *cp1!=';')
  380.         *cp2++ = *cp1++;
  381.     *cp2 = 0;
  382.  
  383.     return(pathp);
  384. }
  385.  
  386. PASCAL NEAR unqname(name)    /* make sure a buffer name is unique */
  387.  
  388. char *name;    /* name to check on */
  389.  
  390. {
  391.     register char *sp;
  392.  
  393.     /* check to see if it is in the buffer list */
  394.     while (bfind(name, 0, FALSE) != NULL) {
  395.  
  396.         /* go to the end of the name */
  397.         sp = name;
  398.         while (*sp)
  399.             ++sp;
  400.         if (sp == name || (*(sp-1) <'0' || *(sp-1) > '8')) {
  401.             *sp++ = '0';
  402.             *sp = 0;
  403.         } else
  404.               *(--sp) += 1;
  405.     }
  406. }
  407.  
  408. /*
  409.  * Ask for a file name, and write the
  410.  * contents of the current buffer to that file.
  411.  * Update the remembered file name and clear the
  412.  * buffer changed flag. This handling of file names
  413.  * is different from the earlier versions, and
  414.  * is more compatable with Gosling EMACS than
  415.  * with ITS EMACS. Bound to "C-X C-W".
  416.  */
  417. PASCAL NEAR filewrite(f, n)
  418. {
  419.     register int s;
  420.     char fname[NFILEN];
  421.  
  422.     if (restflag)        /* don't allow this command if restricted */
  423.         return(resterr());
  424.     if ((s=mlreply(TEXT144, fname, NFILEN)) != TRUE)
  425. /*                     "Write file: " */
  426.         return(s);
  427.     if ((s=writeout(fname)) == TRUE) {
  428.         strcpy(curbp->b_fname, fname);
  429.         curbp->b_flag &= ~BFCHG;
  430.         /* Update mode lines.    */
  431.         upmode();
  432.     }
  433.     return(s);
  434. }
  435.  
  436. /*
  437.  * Save the contents of the current
  438.  * buffer in its associatd file. Do nothing
  439.  * if nothing has changed (this may be a bug, not a
  440.  * feature). Error if there is no remembered file
  441.  * name for the buffer. Bound to "C-X C-S". May
  442.  * get called by "C-Z".
  443.  */
  444. PASCAL NEAR filesave(f, n)
  445. {
  446.     register int s;
  447.  
  448.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  449.         return(rdonly());    /* we are in read only mode    */
  450.     if ((curbp->b_flag&BFCHG) == 0)     /* Return, no changes.    */
  451.         return(TRUE);
  452.     if (curbp->b_fname[0] == 0) {        /* Must have a name.    */
  453.         mlwrite(TEXT145);
  454. /*                      "No file name" */
  455.         return(FALSE);
  456.     }
  457.  
  458.     /* complain about truncated files */
  459.     if ((curbp->b_flag&BFTRUNC) != 0) {
  460.         if (mlyesno(TEXT146) == FALSE) {
  461. /*                          "Truncated file..write it out" */
  462.             mlwrite(TEXT8);
  463. /*                              "[Aborted]" */
  464.             return(FALSE);
  465.         }
  466.     }
  467.  
  468.     /* complain about narrowed buffers */
  469.     if ((curbp->b_flag&BFNAROW) != 0) {
  470.         if (mlyesno(TEXT147) == FALSE) {
  471. /*                          "Narrowed Buffer..write it out" */
  472.             mlwrite(TEXT8);
  473. /*                              "[Aborted]" */
  474.             return(FALSE);
  475.         }
  476.     }
  477.  
  478.     if ((s=writeout(curbp->b_fname)) == TRUE) {
  479.         curbp->b_flag &= ~BFCHG;
  480.         /* Update mode lines.    */
  481.         upmode();
  482.     }
  483.     return(s);
  484. }
  485.  
  486. /*
  487.  * This function performs the details of file writing. It uses
  488.  * the file management routines in the "fileio.c" package. The
  489.  * number of lines written is displayed. Several errors are
  490.  * posible, and cause writeout to return a FALSE result. When
  491.  * $ssave is TRUE,  the buffer is written out to a temporary
  492.  * file, and then the old file is unlinked and the temporary
  493.  * renamed to the original name.  Before the file is written,
  494.  * a user specifyable routine (in $writehook) can be run.
  495.  */
  496.  
  497. PASCAL NEAR writeout(fn)
  498.  
  499. char *fn;    /* name of file to write current buffer to */
  500.  
  501. {
  502.     register LINE *lp;    /* line to scan while writing */
  503.     register char *sp;    /* temporary string pointer */
  504.     register int nline;    /* number of lines written */
  505.     int status;        /* return status */
  506.     int sflag;        /* are we safe saving? */
  507.     char tname[NSTRING];    /* temporary file name */
  508.     char buf[NSTRING];    /* message buffer */
  509.  
  510.     /* let a user macro get hold of things...if he wants */
  511.     execkey(&writehook, FALSE, 1);
  512.  
  513.     /* determine if we will use the save method */
  514.     sflag = FALSE;
  515.     if (ssave && fexist(fn))
  516.         sflag = TRUE;
  517.  
  518. #if    CRYPT
  519.     /* set up for file encryption */
  520.     status = resetkey();
  521.     if (status != TRUE)
  522.         return(status);
  523. #endif
  524.  
  525.     /* turn off ALL keyboard translation in case we get a dos error */
  526.     TTkclose();
  527.  
  528.     /* Perform Safe Save..... */
  529.     if (sflag) {
  530.         /* duplicate original file name, and find where to trunc it */
  531.         sp = tname + (makename(tname, fn) - fn) + 1;
  532.         strcpy(tname, fn);
  533.  
  534.         /* create a unique name, using random numbers */
  535.         do {
  536.             *sp = 0;
  537.             strcat(tname, int_asc(ernd()));
  538.         } while(fexist(tname));
  539.  
  540.         /* open the temporary file */
  541. #if     AOSVS
  542.                 status = ffwopen(fn, tname);
  543. #else
  544.         status = ffwopen(tname);
  545. #endif
  546.     } else
  547. #if     AOSVS
  548.                 status = ffwopen(fn, NULL);
  549. #else
  550.         status = ffwopen(fn);
  551. #endif
  552.  
  553.     /* if the open failed.. clean up and abort */
  554.     if (status != FIOSUC) {
  555.         TTkopen();
  556.         return(FALSE);
  557.     }
  558.  
  559.     /* write the current buffer's lines to the open disk file */
  560.     mlwrite(TEXT148);    /* tell us that we're writing */
  561. /*              "[Writing...]" */
  562.     lp = lforw(curbp->b_linep);    /* start at the first line.    */
  563.     nline = 0;            /* track the Number of lines    */
  564.     while (lp != curbp->b_linep) {
  565.         if ((status = ffputline(&lp->l_text[0], llength(lp))) != FIOSUC)
  566.             break;
  567.         ++nline;
  568.         lp = lforw(lp);
  569.     }
  570.  
  571.  
  572.     /* report on status of file write */
  573.     *buf = 0;
  574.     status |= ffclose();
  575.     if (status == FIOSUC) {
  576.         /* report on success (or lack therof) */
  577.         strcpy(buf, TEXT149);
  578. /*                          "[Wrote " */
  579.         strcat(buf, int_asc(nline));
  580.         strcat(buf, TEXT143);
  581. /*                          " line" */
  582.         if (nline > 1)
  583.             strcat(buf, "s");
  584.  
  585.         if (sflag) {
  586.             /* erase original file */
  587.             /* rename temporary file to original name */
  588.             if (unlink(fn) == 0 && rename(tname, fn) == 0)
  589.                 ;
  590.             else {
  591.                 strcat(buf, TEXT150);
  592. /*                                          ", saved as " */
  593.                 strcat(buf, tname);
  594.                 status = FIODEL;        /* failed */
  595.             }
  596.         }
  597.         strcat(buf, "]");
  598.         mlwrite(buf);
  599.     }
  600.  
  601.     /* reopen the keyboard, and return our status */
  602.     TTkopen();
  603.     return(status == FIOSUC);
  604. }
  605.  
  606. /*
  607.  * The command allows the user
  608.  * to modify the file name associated with
  609.  * the current buffer. It is like the "f" command
  610.  * in UNIX "ed". The operation is simple; just zap
  611.  * the name in the BUFFER structure, and mark the windows
  612.  * as needing an update. You can type a blank line at the
  613.  * prompt if you wish.
  614.  */
  615.  
  616. PASCAL NEAR filename(f, n)
  617.  
  618. {
  619.     register int    s;
  620.     char        fname[NFILEN];
  621.  
  622.     if (restflag)        /* don't allow this command if restricted */
  623.         return(resterr());
  624.     if ((s=mlreply(TEXT151, fname, NFILEN)) == ABORT)
  625. /*                     "Name: " */
  626.         return(s);
  627.     if (s == FALSE)
  628.         strcpy(curbp->b_fname, "");
  629.     else
  630.         strcpy(curbp->b_fname, fname);
  631.     /* Update mode lines.    */
  632.     upmode();
  633.     curbp->b_mode &= ~MDVIEW;      /* no longer read only mode */
  634.     return(TRUE);
  635. }
  636.  
  637. /*
  638.  * Insert file "fname" into the current
  639.  * buffer, Called by insert file command. Return the final
  640.  * status of the read.
  641.  */
  642. PASCAL NEAR ifile(fname)
  643. char    fname[];
  644. {
  645.     register LINE *lp0;
  646.     register LINE *lp1;
  647.     register LINE *lp2;
  648.     register int i;
  649.     register BUFFER *bp;
  650.     register int s;
  651.     register int nbytes;
  652.     register int nline;
  653.     int cmark;    /* current mark */
  654.     char mesg[NSTRING];
  655.  
  656.     bp = curbp;                /* Cheap.        */
  657.     bp->b_flag |= BFCHG;            /* we have changed    */
  658.     bp->b_flag &= ~BFINVS;            /* and are not temporary*/
  659.     if ((s=ffropen(fname)) == FIOERR)    /* Hard file open.    */
  660.         goto out;
  661.     if (s == FIOFNF) {            /* File not found.    */
  662.         mlwrite(TEXT152);
  663. /*                      "[No such file]" */
  664.         return(FALSE);
  665.     }
  666.     mlwrite(TEXT153);
  667. /*              "[Inserting file]" */
  668.  
  669. #if    CRYPT
  670.     s = resetkey();
  671.     if (s != TRUE)
  672.         return(s);
  673. #endif
  674.     /* back up a line and save the mark here */
  675.     curwp->w_dotp = lback(curwp->w_dotp);
  676.     curwp->w_doto = 0;
  677.     for (cmark = 0; cmark < NMARKS; cmark++) {
  678.         curwp->w_markp[cmark] = curwp->w_dotp;
  679.         curwp->w_marko[cmark] = 0;
  680.     }
  681.  
  682.     nline = 0;
  683.     while ((s=ffgetline()) == FIOSUC) {
  684.         nbytes = strlen(fline);
  685.         if ((lp1=lalloc(nbytes)) == NULL) {
  686.             s = FIOMEM;        /* Keep message on the    */
  687.             break;            /* display.        */
  688.         }
  689.         lp0 = curwp->w_dotp;  /* line previous to insert */
  690.         lp2 = lp0->l_fp;    /* line after insert */
  691.  
  692.         /* re-link new line between lp0 and lp2 */
  693.         lp2->l_bp = lp1;
  694.         lp0->l_fp = lp1;
  695.         lp1->l_bp = lp0;
  696.         lp1->l_fp = lp2;
  697.  
  698.         /* and advance and write out the current line */
  699.         curwp->w_dotp = lp1;
  700.         for (i=0; i<nbytes; ++i)
  701.             lputc(lp1, i, fline[i]);
  702.         ++nline;
  703.     }
  704.     ffclose();                /* Ignore errors.    */
  705.     curwp->w_markp[0] = lforw(curwp->w_markp[0]);
  706.     strcpy(mesg, "[");
  707.     if (s==FIOERR) {
  708.         strcat(mesg, TEXT141);
  709. /*                           "I/O ERROR, " */
  710.         curbp->b_flag |= BFTRUNC;
  711.     }
  712.     if (s == FIOMEM) {
  713.         strcat(mesg, TEXT142);
  714. /*                           "OUT OF MEMORY, " */
  715.         curbp->b_flag |= BFTRUNC;
  716.     }
  717.     strcat(mesg, TEXT154);
  718. /*                   "Inserted " */
  719.     strcat(mesg, int_asc(nline));
  720.     strcat(mesg, TEXT143);
  721. /*                   " line" */
  722.     if (nline > 1)
  723.         strcat(mesg, "s");
  724.     strcat(mesg, "]");
  725.     mlwrite(mesg);
  726.  
  727. out:
  728.     /* advance to the next line and mark the window for changes */
  729.     curwp->w_dotp = lforw(curwp->w_dotp);
  730.     curwp->w_flag |= WFHARD | WFMODE;
  731.  
  732.     /* copy window parameters back to the buffer structure */
  733.     curbp->b_dotp = curwp->w_dotp;
  734.     curbp->b_doto = curwp->w_doto;
  735.     for (cmark = 0; cmark < NMARKS; cmark++) {
  736.         curbp->b_markp[cmark] = curwp->w_markp[cmark];
  737.         curbp->b_marko[cmark] = curwp->w_marko[cmark];
  738.     }
  739.     curbp->b_fcol = curwp->w_fcol;
  740.  
  741.     if (s == FIOERR)            /* False if error.    */
  742.         return(FALSE);
  743.     return(TRUE);
  744. }
  745.